INSTALL_DIR   = @INSTALL_DIR@

BUILD_VERSION := $(shell @GIT@ describe --tags)

CC            = @CC@

INCLUDE_PCAP  = @PCAP_CFLAGS@

INCLUDE_OTHER = -Ithirdparty \
                @GLIB2_CFLAGS@ \
	        @YARA_CFLAGS@ \
	        @MAXMINDDB_CFLAGS@ \
	        @MAGIC_CFLAGS@ \
		@NGHTTP2_CFLAGS@ \
	        @ZSTD_CFLAGS@ \
	        @CURL_CFLAGS@


LIB_PCAP      = @PCAP_LIBS@
LIB_SNF       = /opt/snf/lib/*.so
LIB_OTHER     = @GLIB2_LIBS@ \
	        @YARA_LIBS@ \
	        @MAXMINDDB_LIBS@ \
	        @CURL_LIBS@ \
	        @NGHTTP2_LIBS@ \
	        @ZSTD_LIBS@ \
                @LIBS@ \
	        thirdparty/http_parser.o \
	        thirdparty/js0n.o \
	        thirdparty/patricia.o \
		@DL_LIB@ -lssl -lcrypto -lyaml

C_FILES         = main.c db.c yara.c http.c config.c parsers.c plugins.c field.c writers.c writer-inplace.c writer-null.c writer-simple.c readers.c reader-libpcap-file.c reader-libpcap.c reader-tpacketv3.c reader-null.c reader-pcapoverip.c reader-tzsp.c reader-scheme.c reader-scheme-file.c reader-scheme-http.c reader-scheme-s3.c packet.c session.c rules.c drophash.c pq.c dedup.c
O_FILES         = $(C_FILES:.c=.o)

INSTALL         = @INSTALL@
bindir          = @prefix@/bin
installinclude  = @prefix@/include

SANITIZE_LDFLAGS = -fno-common -fsanitize=address -fsanitize=integer -fsanitize=nullability
SANITIZE_CFLAGS  = $(SANITIZE_LDFLAGS)

FUZZ_LDFLAGS     = -fno-common -fsanitize=fuzzer,address -fstack-protector-all -fsanitize-address-use-after-scope
FUZZ_CFLAGS      = -DFUZZLOCH $(FUZZ_LDFLAGS)

%.o : %.c arkime.h hash.h dll.h bsb.h
	$(CC) -fno-strict-aliasing -pthread -fPIC -g -O2 $(EXTRA_CFLAGS) -Wall -Wextra -D_GNU_SOURCE -std=gnu99 -c $< \
            $(INCLUDE_PCAP) \
            $(INCLUDE_OTHER) \
            -DBUILD_VERSION='"$(BUILD_VERSION)"'

all:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o $(O_FILES)
	$(CC) $(EXTRA_LDFLAGS) -rdynamic -ggdb -pthread $(O_FILES) -o capture \
            @UNDEFINED_FLAGS@ \
	    $(LIB_PCAP) \
	    $(LIB_OTHER) \
	    -lm @RESOLV_LIB@ @MAGIC_LIBS@ -lffi -lz
	(cd parsers; $(MAKE) EXTRA_CFLAGS="$(EXTRA_CFLAGS)")
	(cd plugins; $(MAKE) EXTRA_CFLAGS="$(EXTRA_CFLAGS)")

sanitize:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	touch arkime.h
	$(MAKE) EXTRA_CFLAGS="$(SANITIZE_CFLAGS)" EXTRA_LDFLAGS="$(SANITIZE_LDFLAGS)"
	touch arkime.h

fuzzloch:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	touch arkime.h
	$(MAKE) EXTRA_CFLAGS="$(FUZZ_CFLAGS)" EXTRA_LDFLAGS="$(FUZZ_LDFLAGS)"
	mv capture fuzzloch-capture
	touch arkime.h

sfuzzloch:thirdparty/js0n.o thirdparty/http_parser.o thirdparty/patricia.o
	touch arkime.h
	$(MAKE) EXTRA_CFLAGS="-DSFUZZLOCH $(FUZZ_CFLAGS)" EXTRA_LDFLAGS="$(FUZZ_LDFLAGS)"
	mv capture fuzzloch-capture
	touch arkime.h

thirdparty/js0n.o:thirdparty/js0n.c
	$(CC) -fno-strict-aliasing -pthread -fPIC -O2 -c thirdparty/js0n.c -o thirdparty/js0n.o

thirdparty/patricia.o:thirdparty/patricia.c
	$(CC) -fno-strict-aliasing -pthread -fPIC -O2 -c thirdparty/patricia.c -o thirdparty/patricia.o

thirdparty/http_parser.o: thirdparty/http_parser.c
	$(CC) -fno-strict-aliasing -pthread -fPIC -DNDEBUG -DHTTP_PARSER_STRICT=0 -DHTTP_PARSER_DEBUG=0 @CFLAGS@ -c thirdparty/http_parser.c -o thirdparty/http_parser.o

install: installdirs
	@mkdir -p $(installinclude)
	$(INSTALL) *.h thirdparty/http_parser.h $(installinclude)
	$(INSTALL) capture $(bindir)/capture

installdirs:
	$(INSTALL) -d $(bindir)
	(cd parsers; $(MAKE) install)
	(cd plugins; $(MAKE) install)

check:
	(cd plugins; $(MAKE) check)

distclean realclean clean:
	rm -f *.o arkime-capture capture */*.o */*.so

cppcheck:
	cppcheck -q --enable=all --disable=missingInclude --std=c99 -I. -Ithirdparty $(INCLUDE_OTHER) *.c plugins/*.c plugins/*/*.c parsers/*.c

format:
	AStyle --options=.astylerc  *.[ch]  p*/*.c p*/*/*.c  ../tests/plugins/*.c
